package com.maple.phoneward.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.entity.Contacts;
import com.maple.phoneward.entity.SmsInfo;

/**
 * AppUtils.java
 * 
 * @author: YaJian Qiu
 * @Time 2013-3-20 下午12:04:15<br>
 *       本类描述: 应用工具类
 */
@SuppressWarnings("deprecation")
public final class AppUtil {

	private static final String TAG = "AppUtils";

	/**
	 * 备份数据
	 * 
	 * @param context
	 */
	public static final void backupData(Context context) {
		HashMap<String, ArrayList<Contacts>> map = AppUtil.getContacts(context, Constants.TYPE_PHONE);
		ArrayList<Contacts> list = map.get(Constants.TYPE_PHONE);
		StringBuffer cSb = new StringBuffer();
		for (Contacts c : list) {
			cSb.append(c.getDisplayName()).append("  ").append(c.getNumber()).append("\n");
		}
		File dir = new File(Constants.APP_DATA_DIR + "backup");
		if (!dir.exists())
			dir.mkdirs();
		final File sFile = new File(dir, "365防盗卫士----联系人备份.txt");
		FileUtil.saveStringToFile(sFile, cSb.toString());

		ArrayList<SmsInfo> smsInfos = AppUtil.getSmsInfo(context, Constants.SMS_URI_ALL);
		StringBuffer mSb = new StringBuffer();
		for (SmsInfo s : smsInfos) {
			mSb.append(s.getPerson()).append("  ").append(s.getAddress()).append(StringUtil.dateToString(s.getDate(), "yyyy-MM-dd hh:mm:ss")).append("\n").append(s.getBody())
					.append("\n\n");
		}
		final File mFile = new File(dir, "365防盗卫士---短信息备份.txt");
		FileUtil.saveStringToFile(mFile, mSb.toString());
		EmailUtil.sendEmail(context, "联系人和短信备份", "联系人和短信已备份，详情请查看附件！", sFile.getAbsolutePath(), mFile.getAbsolutePath());
	}

	/**
	 * 获取SharedPreferences对象
	 * 
	 * @param context
	 * @param name
	 * @return SharedPreferences
	 */
	public static final SharedPreferences getSharedPreferences(Context context, String name) {
		if (StringUtil.isEmpty(name)) {
			return PreferenceManager.getDefaultSharedPreferences(context);
		} else {
			return context.getSharedPreferences(name, Context.MODE_APPEND);
		}
	}

	/**
	 * 获取Editor对象
	 * 
	 * @param context
	 * @return Editor
	 */
	public static final Editor getEditor(Context context) {
		return getSharedPreferences(context, Constants.PREFERENCE_NAME).edit();
	}

	/**
	 * 从SharedPreferences获取值
	 * 
	 * @param context
	 * @param key
	 * @param defaultValue
	 *            默认值
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static final <T> T getFromSharedPreferences(Context context, String key, Object defaultValue, Class<T> clazz) {
		// SharedPreferences sp =
		// PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences sp = getSharedPreferences(context, Constants.PREFERENCE_NAME);
		Object obj = null;
		if (clazz == String.class) {
			obj = sp.getString(key, (String) defaultValue);
		}
		if (clazz == Integer.class) {
			obj = sp.getInt(key, (Integer) defaultValue);
		}
		if (clazz == Float.class) {
			obj = sp.getFloat(key, (Float) defaultValue);
		}
		if (clazz == Boolean.class) {
			obj = sp.getBoolean(key, (Boolean) defaultValue);
		}
		if (clazz == Long.class) {
			obj = sp.getLong(key, (Long) defaultValue);
		}
		return (T) obj;
	}

	public static final String getFromSharedPreferences(Context context, String key) {
		return getFromSharedPreferences(context, key, String.class);
	}

	/**
	 * 从SharedPreferences获取值
	 * 
	 * @param context
	 * @param key
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static final <T> T getFromSharedPreferences(Context context, String key, Class<T> clazz) {
		// SharedPreferences sp =
		// PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences sp = getSharedPreferences(context, Constants.PREFERENCE_NAME);
		// Class<T> clazz = null;
		// if (clazzs != null && clazzs.length != 0) {
		// clazz = clazzs[0];
		// } else {
		// clazz = (Class<T>) String.class;
		// }
		Object obj = null;
		if (clazz == String.class) {
			obj = sp.getString(key, "");
			// if ("".equals(obj)) // 从配置文件中获取不到值则从sd卡中获取
			// obj = FileUtil.getConfigFromSDCard(key, clazz);
		}
		if (clazz == Integer.class) {
			obj = sp.getInt(key, 0);
			// if (Integer.valueOf(0) == obj)
			// obj = FileUtil.getConfigFromSDCard(key, clazz);
		}
		if (clazz == Float.class) {
			obj = sp.getFloat(key, 0f);
			// if (Float.valueOf(0f) == obj)
			// obj = FileUtil.getConfigFromSDCard(key, clazz);
		}
		if (clazz == Boolean.class) {
			obj = sp.getBoolean(key, false);
			// if (Boolean.valueOf(false) == obj)
			// obj = FileUtil.getConfigFromSDCard(key, clazz);
		}
		if (clazz == Long.class) {
			obj = sp.getLong(key, 0l);
			// if (Long.valueOf(0l) == obj)
			// obj = FileUtil.getConfigFromSDCard(key, clazz);
		}
		// Logger.d(TAG, "getFromSharedPreferences获取配置：" + key + "=" + (T) obj);
		return (T) obj;
	}

	/**
	 * 从SharedPreferences移除值
	 * 
	 * @param context
	 * @param key
	 */
	public static final void removeFromSharedPreferences(Context context, String key) {
		if (StringUtil.isEmpty(key))
			return;
		Editor editor = getEditor(context);
		editor.remove(key);
		editor.commit();
	}

	public static final void saveToSharedPreferences(Context context, HashMap<String, String> params) {
		if (params == null || params.isEmpty())
			return;
		Set<Entry<String, String>> entrySet = params.entrySet();
		Iterator<Entry<String, String>> it = entrySet.iterator();
		while (it.hasNext()) {
			Entry<String, String> entry = it.next();
			String key = entry.getKey();
			String value = entry.getValue();
			saveToSharedPreferences(context, key, value);
		}
	}

	/**
	 * 将键值对保存到SharedPreferences中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static final void saveToSharedPreferences(Context context, String key, Object value) {
		if (context == null || key == null)
			return;
		// 如果是防盗密码则加密后再保存
		if (Constants.CONFIG_PHONEWARD_PASSWORD.equals(key)) {
			value = Md5Util.md5(value);
		}
		Editor editor = getEditor(context);
		if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		}
		if (value instanceof Integer) {
			editor.putInt(key, (Integer) value);
		}
		if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		}
		if (value instanceof Long) {
			editor.putLong(key, (Long) value);
		}
		if (value instanceof String) {
			editor.putString(key, StringUtil.trim((String) value));
		}
		editor.commit();
		Logger.d(TAG, "saveToSharedPreferences保存配置：" + key + "=" + value);
		// 同步保存一份到sd,防止用户清除了数据
		// FileUtil.saveConfigToSDCard(key, value.toString());
	}

	public static final List<RunningTaskInfo> getTopActivity(Context context) {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
		for (RunningTaskInfo t : tasks) {
			System.out.println(t.topActivity.getPackageName());
		}
		return tasks;
	}

	/**
	 * 设置手机情景模式
	 * 
	 * @param context
	 * @param model
	 */
	public static final void setRingerMode(Context context, int model) {
		// 播放声音时将音量调到最大
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_PLAY_SOUND);
		audioManager.setRingerMode(model);
	}

	/**
	 * 播放声音
	 * 
	 * @param context
	 * @param resid
	 * @return MediaPlayer
	 */
	public static final MediaPlayer playSound(Context context, int resid) {
		MediaPlayer mPlayer = MediaPlayer.create(context, resid);
		if (mPlayer != null && !mPlayer.isPlaying()) {
			setRingerMode(context, AudioManager.RINGER_MODE_NORMAL);
			mPlayer.start();
		}
		return mPlayer;
	}

	/**
	 * 播放声音
	 * 
	 * @param context
	 * @param resid
	 * @param isLooping
	 *            是否循环播放
	 * @return
	 */
	public static final MediaPlayer playSound(Context context, int resid, boolean isLooping) {
		MediaPlayer mPlayer = playSound(context, resid);
		mPlayer.setLooping(isLooping);
		return mPlayer;
	}

	/**
	 * 停止播放声音
	 * 
	 * @param mPlayer
	 */
	public static final void stopSound(MediaPlayer mPlayer) {
		if (mPlayer != null && mPlayer.isPlaying()) {
			mPlayer.stop();
		}
	}

	/**
	 * @param targetNumber
	 * @param toSend
	 * @return 发送的短息的条数
	 */
	public static final int sendMessage(String targetNumber, String toSend) {
		int count = 0;
		if (StringUtil.isEmpty(targetNumber) || StringUtil.isEmpty(toSend)) {
			return count;
		}
		SmsManager smsManager = SmsManager.getDefault();
		// try {
		// // msg = new String(msg.getBytes(),"US-ASCII");
		// toSend = new String(toSend.getBytes(),"unicode");
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }
		ArrayList<String> list = smsManager.divideMessage(toSend);
		for (String msg : list) {
			smsManager.sendTextMessage(targetNumber, null, msg, null, null);
			count++;
		}
		Logger.d(TAG, "发送短信：targetNumber=[" + targetNumber + "] toSend=[" + toSend + "]");
		return count;
	}

	/**
	 * 删除所有短信息
	 * 
	 * @param context
	 * @return 影响的数目
	 */
	public static final int deleteSmsInfo(Context context) {
		ContentResolver cr = context.getContentResolver();
		return cr.delete(Uri.parse(Constants.SMS_URI_ALL), null, null);
	}

	/**
	 * @param context
	 * @param smsTypeUri
	 *            信息类型，收件箱，发件箱，草稿箱 。 参照Constants
	 * @return 手机短信
	 */
	public static final ArrayList<SmsInfo> getSmsInfo(Context context, String smsTypeUri) {

		ArrayList<SmsInfo> smsList = new ArrayList<SmsInfo>();
		try {
			ContentResolver cr = context.getContentResolver();
			// String[] projection = new String[] { "_id", "address", "person",
			// "body", "date", "type" };
			Cursor cur = cr.query(Uri.parse(smsTypeUri), null, null, null, "date desc");
			while (cur.moveToNext()) {
				Long id = cur.getLong(cur.getColumnIndex("_id"));
				String person = cur.getString(cur.getColumnIndex("person"));
				String address = cur.getString(cur.getColumnIndex("address"));
				String body = cur.getString(cur.getColumnIndex("body"));
				Date date = StringUtil.stringToDate(cur.getString(cur.getColumnIndex("date")), "yyyyMMddhhmmss");
				Integer type = cur.getInt(cur.getColumnIndex("type"));
				Integer read = cur.getInt(cur.getColumnIndex("read"));
				SmsInfo info = new SmsInfo(id, address, person, type, date, body, read);
				smsList.add(info);
			}
			if (cur != null) {
				cur.close();
			}
		} catch (SQLiteException ex) {
			Logger.e(TAG, "获取手机短信息出错" + ex.getMessage());
		}
		return smsList;
	}

	/**
	 * 根据关键字删除短信
	 * 
	 * @param context
	 * @param keyword
	 * @return
	 */
	public static final int deleteSmsByKeyword(Context context, String keyword) {
		ContentResolver cr = context.getContentResolver();
		Logger.d(TAG, "删除指令短信:" + keyword);
		return cr.delete(Uri.parse(Constants.SMS_URI_ALL), " body like ?", new String[] { "%" + keyword + "%" });
	}

	/**
	 * 删除所有短信
	 * 
	 * @param context
	 * @param keyword
	 * @return
	 */
	public static final int deleteAllSms(Context context, String keyword) {
		ContentResolver cr = context.getContentResolver();
		return cr.delete(Uri.parse(Constants.SMS_URI_ALL), null, null);
	}

	/**
	 * 删除所有联系人
	 * 
	 * @param context
	 * @return 影响的数目
	 */
	public static final int deleteContacts(Context context) {
		ContentResolver cr = context.getContentResolver();
		ArrayList<Long> ids = getContactsId(context);
		int n = 0;
		for (Long id : ids) {
			Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
			cr.delete(uri, null, null);
			n++;
		}
		Logger.d(TAG, "删除"+n+"条联系人");
		return n;
	}

	public static final ArrayList<Long> getContactsId(Context context) {

		ContentResolver cr = context.getContentResolver();
		// 取得电话本中开始一项的光标
		Cursor cursor = cr.query(Phone.CONTENT_URI, null, null, null, null);
		ArrayList<Long> ids = new ArrayList<Long>();
		while (cursor.moveToNext()) {
			// 取得联系人ID
			Long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
			ids.add(id);
			Logger.d(TAG, "联系人id="+id);
		}
		if (cursor != null) {
			cursor.close();
		}
		return ids;
	}
	
	/**
	 * 获取联系人
	 * 
	 * @param context
	 * @param type
	 *            Constants.TYPE_PHONE来自手机，Constants.TYPE_SIM来自SIM卡，null所有
	 * @return
	 */
	public static final HashMap<String, ArrayList<Contacts>> getContacts(Context context, String type) {

		ContentResolver cr = context.getContentResolver();
		// 取得电话本中开始一项的光标
		Cursor cursor = cr.query(Phone.CONTENT_URI, null, null, null, null);
		HashMap<String, ArrayList<Contacts>> map = new HashMap<String, ArrayList<Contacts>>();
		ArrayList<Contacts> phoneList = null;
		ArrayList<Contacts> simList = null;
		if (type == null) {
			phoneList = new ArrayList<Contacts>();
			simList = new ArrayList<Contacts>();
			map.put(Constants.TYPE_PHONE, phoneList);
			map.put(Constants.TYPE_SIM, simList);
		} else if (Constants.TYPE_SIM.equals(type)) {
			simList = new ArrayList<Contacts>();
			map.put(Constants.TYPE_SIM, simList);
		} else if (Constants.TYPE_PHONE.equals(type)) {
			phoneList = new ArrayList<Contacts>();
			map.put(Constants.TYPE_PHONE, phoneList);
		}
		while (cursor.moveToNext()) {
			// 取得联系人ID
			Long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
			String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
			String num = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
			String numType = cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
			if (type == null) {
				Contacts c = new Contacts();
				c.setContactId(id);
				c.setDisplayName(name);
				c.setNumber(num);
				c.setType(Contacts.getType(numType));
				c.setTypeCode(Contacts.getTypeCode(numType));
				if (Contacts.getTypeCode(numType) == Constants.TYPE_PHONE_CODE) {
					phoneList.add(c);
				}
				if (Contacts.getTypeCode(numType) == Constants.TYPE_SIM_CODE) {
					simList.add(c);
				}
				Logger.d(TAG, "联系人ID=[" + id + "]                  名称=[" + name + "]                  号码=[" + num + "]                  号码类型=[" + Contacts.getType(numType) + "]");
			}
			if (Constants.TYPE_SIM.equals(type)) {
				if (numType.contains(type)) {
					Contacts c = new Contacts();
					c.setContactId(id);
					c.setDisplayName(name);
					c.setNumber(num);
					c.setType(Contacts.getType(numType));
					c.setTypeCode(Contacts.getTypeCode(numType));
					simList.add(c);
					Logger.d(TAG, "联系人ID=[" + id + "]                  名称=[" + name + "]                  号码=[" + num + "]                  号码类型=[" + Contacts.getType(numType)
							+ "]");
				}
			} else if (Constants.TYPE_PHONE.equals(type)) {
				if (numType.contains(Constants.TYPE_SIM)) {
					continue;
				}
				Contacts c = new Contacts();
				c.setContactId(id);
				c.setDisplayName(name);
				c.setNumber(num);
				c.setType(Contacts.getType(numType));
				c.setTypeCode(Contacts.getTypeCode(numType));
				phoneList.add(c);
				Logger.d(TAG, "联系人ID=[" + id + "]                  名称=[" + name + "]                  号码=[" + num + "]                  号码类型=[" + Contacts.getType(numType) + "]");
			}

		}
		if (cursor != null) {
			cursor.close();
		}
		return map;
	}

	/**
	 * @param context
	 * @return SIM卡是否更换
	 */
	public static final boolean isSimChange(Context context) {

		return false;
	}

	/**
	 * @param context
	 * @return SIM是否可用
	 */
	public static final boolean isSimReady(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		boolean value = manager.getSimState() == TelephonyManager.SIM_STATE_READY;
		Logger.d(TAG, "SIM卡是否可用:" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 本机号码
	 */
	public static final String getPhoneNumber(Context context) {
		isSimReady(context);
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String value = manager.getLine1Number();
		Logger.d(TAG, "本机号码：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 安全号码1
	 */
	public static final String getSafetyPhoneNumber1(Context context) {
		String value = getFromSharedPreferences(context, Constants.CONFIG_SAFETY_PHONE_NUMBER_1, String.class);
		Logger.d(TAG, "安全号码1：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 安全号码2
	 */
	public static final String getSafetyPhoneNumber2(Context context) {
		String value = getFromSharedPreferences(context, Constants.CONFIG_SAFETY_PHONE_NUMBER_2, String.class);
		Logger.d(TAG, "安全号码2：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 本机IMEI
	 */
	public static final String getDeviceId(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String value = manager.getDeviceId();
		Logger.d(TAG, "本机IMEI：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return SIM卡运营商名称，如CMCC
	 */
	public static final String getSimOperatorName(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String value = manager.getSimOperatorName();
		Logger.d(TAG, "SIM卡运营商名称：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 手机型号
	 */
	public static final String getPhoneType() {
		String value = Build.MODEL;
		Logger.d(TAG, "手机型号：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return 手机系统版本号
	 */
	public static final String getSystemVersion() {
		String value = Build.VERSION.RELEASE;
		Logger.d(TAG, "手机系统版本：" + value);
		return value;
	}

	/**
	 * @param context
	 * @return SIM卡唯一标识序列号
	 */
	public static final String getSimSerialNumber(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String value = manager.getSimSerialNumber();
		Logger.d(TAG, "SIM卡序列号：" + value);
		return value;
	}

	/**
	 * 设置无标题，要在setContentView方法前调用
	 * 
	 * @param activity
	 */
	public static final void setNotTitle(Activity activity) {
		// 设置无标题
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	/**
	 * 设置全屏，要在setContentView方法前调用
	 * 
	 * @param activity
	 */
	public static final void setFullScreen(Activity activity) {
		// 设置无标题
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置全屏
		activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public static final String[] getStringArray(Context context, int resId) {
		try {
			return context.getResources().getStringArray(resId);
		} catch (NotFoundException e) {
			e.printStackTrace();
			return new String[0];
		}
	}

	public static final String getString(Context context, int resId) {
		try {
			return context.getResources().getString(resId);
		} catch (NotFoundException e) {
			e.printStackTrace();
			return "resouce not found";
		}
	}

	/**
	 * @param context
	 * @param msg
	 */
	public static final void showShortMessage(Context context, Object msg) {
		if (msg instanceof Integer) {
			Object temp = msg;
			msg = getString(context, (Integer) msg);
			if (StringUtil.equals(msg.toString(), "resouce not found")) {
				msg = temp;
			}
		}

		Toast.makeText(context, msg.toString(), Toast.LENGTH_SHORT).show();
	}

	/**
	 * @param context
	 * @param msg
	 */
	public static final void showLongMessage(Context context, Object msg) {
		if (msg instanceof Integer) {
			Object temp = msg;
			msg = getString(context, (Integer) msg);
			if (StringUtil.equals(msg.toString(), "resouce not found")) {
				msg = temp;
			}
		}
		Toast.makeText(context, msg.toString(), Toast.LENGTH_LONG).show();
	}

	/**
	 * @param email
	 * @return 是否符合邮箱格式
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}

	/**
	 * @param email
	 * @return 是否符合手机号码格式
	 */
	public static boolean isPhoneNumber(String num) {
		String str = "^1[3-8]\\d{9}$";// 匹配手机号码
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(num);
		return m.matches();
	}

	/**
	 * @param email
	 * @return 是否符合密码格式（ 6-12位字母数字组合）
	 */
	public static boolean isPassword(String pwd) {
		String str = "^[\\w]{6,12}$";// 6-12位字母数字组合
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(pwd);
		return m.matches();
	}
}
